package com.xbq.exceleditor.adapter

import com.blankj.utilcode.constant.TimeConstants
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xbq.exceleditor.R
import com.xbq.exceleditor.databinding.ItemVideoCollectionBinding
import com.xbq.xbqsdk.net.officeeditor.vo.OfficeVideoCollection
import com.xbq.xbqsdk.util.format.FormatUtils
import java.util.*
import javax.inject.Inject
import kotlin.random.Random

/**
 * Author: liaohaiping
 * Time: 2021-01-20
 * Description:
 */
class JinxuanCollectionsAdapter @Inject constructor() :
    BaseQuickAdapter<OfficeVideoCollection, BaseViewHolder>(R.layout.item_video_collection) {
    val idSets = hashSetOf<Int>()
    val random by lazy { Random(context.packageName.hashCode()) }
    override fun convert(
        holder: BaseViewHolder,
        item: OfficeVideoCollection,
    ) {
        ItemVideoCollectionBinding.bind(holder.itemView).let {
            it.videoTitle.text = item.title
            val timeSpanByNow =
                com.blankj.utilcode.util.TimeUtils.getTimeSpanByNow(Date(2021 - 1900, 0, 21),
                    TimeConstants.DAY)
            var increment = timeSpanByNow * random.nextInt(10, 100)
            val count = random.nextInt(1000, 1000000) + increment;
            it.videoDesc.text = "${FormatUtils.formatCount(count)}人学习过"

            val imgId = context.resources.getIdentifier(
                "ic_video_collection_${generateId(random)}",
                "drawable",
                context.packageName)
            Glide.with(context)
                .load(imgId)
                .into(it.videoImage)
        }
    }


    fun setNewDatas(datas: List<OfficeVideoCollection>){
        idSets.clear()
        setList(datas)
    }

    private fun generateId(random: Random): Int {
        var index = random.nextInt(2, 17)
        while (idSets.contains(index)) {
            index = random.nextInt(2, 17)
        }
        idSets.add(index)
        return index
    }


}